/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.Interest;
import Beans.UserInterest;
import dao.UserInterestDao;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**

 @author jtanguay
 */
@ManagedBean(name = "userInterestManager")
@RequestScoped
public class UserInterestManager {

  //Attribut
  private UserInterest userInterest;

  public UserInterestManager() {
  }

  //Getter et setter
  public UserInterest getUserInterest() {
    return userInterest;
  }

  public void setUserInterest(UserInterest userInterest) {
    this.userInterest = userInterest;
  }

  @PostConstruct
  public void init() {
    userInterest = new UserInterest();

  }

  //Recuperer les interet pour un user
  public static List<UserInterest> getInterestForUser(int idUser) {

    List<UserInterest> userInterestList = UserInterestDao.getAllInterestForUser(idUser);

    return userInterestList;
  }

  //Inserer un interet
  public static int insertInterestForUser(int idUser, int idInterest) {

    int interest = UserInterestDao.insertAnInterest(idUser, idInterest);

    return interest;
  }

  //Supprimer un interet
  public static int deleteInterestForUser(int idUser, int idInterest) {

    int deleteInterest = UserInterestDao.deleteUserInterest(idUser, idInterest);

    return deleteInterest;
  }

  //Recuperer les nom des interets
  public List<String> getInterestNameForUser(int idUser) {

    List<UserInterest> userInterestList = UserInterestDao.getAllInterestForUser(idUser);
    List<Interest> allInterest = InterestManager.getAllInterest();
    List<String> userInterestNameList = new ArrayList<String>();

    //Boucle pour tous les interest
    for (Interest interest : allInterest) {
      //Boucle pour les interet choisi
      for (UserInterest ui : userInterestList) {
        if (interest.getIdInterest() == ui.getIdInterest()) {
          userInterestNameList.add(interest.getInterestName());
        }
      }
    }

    return userInterestNameList;
  }

}
